package com.example.d15_homework.homework03;

/**
 * 副卡
 */
public class SubCard extends MasterCard {
    // 额外透支属性
    private double overDraft; // 额外透支额度

    // 重写父类的存款方法
    @Override
    public void deposit(double amount) {
        System.out.println("初始 银行卡 - 副卡 余额为：" + getBalance());
        if (amount > 0) {
            double a = getBalance() + amount;
            setBalance(a);
            System.out.println("用 副卡存" + amount + "元卡号为：" + getCardId() + "户主为," + getName() + ",存款成功,余额为：" + getBalance() + "透支额度" + overDraft);
        } else {
            System.out.println("存款失败,存款金额有误");
        }
    }

    // 重写父类的取款方法
    @Override
    public void withdraw(double amount) {
        // 定义一个变量存储缺少的钱
        double lackMoney = 0;
        // 先判断取款的金额超过余额没有
        if (amount > getBalance()) {
            // 此时amount中存的就是还缺少的钱
            lackMoney = getBalance() - amount;
            // 进入这个if语句,说明取款的钱,肯定超过了余额,所以余额直接设置成0.0,然后开始使用透支额度
            setBalance(0.0);
        } else {
            // 否则,说明取款金额没有超过余额,直接从余额中减去即可
            setBalance(getBalance() - amount);
        }
        overDraft = overDraft + lackMoney;

        if (getBalance() == 0) {
            // 开始使用透支额度
            //overDraft = overDraft - amount;
            System.out.println("用 副卡取" + amount + "元卡号为：" + getCardId() + "户主为," + getName() + ",取款成功,余额为：" + getBalance() + "透支额度" + overDraft);
        } else {
            // 正常使用余额支付
            System.out.println("用 副卡取" + amount + "元卡号为：" + getCardId() + "户主为," + getName() + ",取款成功,余额为：" + getBalance() + "透支额度" + overDraft);
        }
    }

    public SubCard() {

    }

    public SubCard(String cardId, String name, double balance, double overDraft) {
        super(cardId, name, balance);
        this.overDraft = overDraft;
    }

    public double getOverDraft() {
        return overDraft;
    }

    public void setOverDraft(double overDraft) {
        this.overDraft = overDraft;
    }
}
